package cn.com.generaldata.os.builder;

import cn.com.generaldata.os.result.WSResult;
import cn.com.generaldata.os.util.ThreadContext;

public class WSResultBuilder<T extends WSResultBuilder<T, U>, U extends WSResult> extends AbstractBuilder<T, U> {

	private static final long serialVersionUID = 1L;

	public WSResultBuilder(U wSResult) {
		super(wSResult);
	}

	@SuppressWarnings("unchecked")
	public T buildResult(boolean success, String code, String[] args, String message) {
		getObject().setResult(success, code, args, message);
		return (T) this;
	}

	public T buildErrorResult(Exception e) {
		String code = (String) ThreadContext.get("code");
		String[] args = (String[]) ThreadContext.get("args");
		String message = (String) ThreadContext.get("message");
		if (code == null) {
			code = e.getClass().getSimpleName();
			args = null;
			message = e.getMessage();
		}
		return buildResult(false, code, args, message);
	}

	public T buildSuccessResult() {
		return buildResult(true, null, null, null);
	}

}
